/*********************************************************************************************************
**
**                                   GJB 标准测试集
**
**                               Copyright All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: gjb_S0101804GN_5.c
**
** 文件创建日期: 2021 年 1 月 12 日
**
** 描        述: 协议栈信息统计功能测试。编制测试用例代码，调用恰当
**               接口函数获取协议栈统计信息--
**
**               udp.
**
**               该用例需要使用 tools/USR-TCP232-Test.exe 工具进行配合测试
**
**               TEST_IP 需要设置为真是的 PC ip
*********************************************************************************************************/

#include <pthread.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#ifndef TEST_IP
#define TEST_IP "192.168.80.14"
#endif

int main()
{
    int s,on = 1;
    int ret;
    socklen_t len;
    struct sockaddr_in srvr,clientAddr;
    char recvBuf[64] = {0};

    memset(&srvr, 0, sizeof(srvr));
    memset(&clientAddr, 0, sizeof(clientAddr));
    memset(recvBuf, 0, 64);
    srvr.sin_family = AF_INET;
    srvr.sin_addr.s_addr = htonl(INADDR_ANY);
    srvr.sin_port = htons(9999);

    clientAddr.sin_family = AF_INET;
    clientAddr.sin_port = htons(9999);
    clientAddr.sin_addr.s_addr = inet_addr(TEST_IP);

    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (s == -1) {
        printf( "socket error!\n");
        return (-1);
    } else {
        printf( "socket success!\n");
    }

    ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
    if (ret == -1) {
        printf( "setsockopt error!\n");
        return (-1);
    } else {
        printf( "setsockopt success!\n");
    }

    ret = bind(s, (struct sockaddr *)&srvr, sizeof(srvr));
    if (ret == -1) {
        printf( "bind error!\n");
        return (-1);

    } else {
        printf( "bind success!\n");
    }

    printf("please start PC UDP server add push \"start\"\n");

    len = sizeof(srvr);

    while (1) {
        ret = recvfrom(s, recvBuf, 64, 0, (struct sockaddr *)&clientAddr, &len);
        if (ret == -1) {
            printf( "recvfrom error!\n");
            return (-1);
        } else {
            printf("recvbuf is %s\n",recvBuf);
            printf( "recvfrom success!\n");
        }

        printf("display UDP information:\n");
        system("netstat -u -na");
        // udp_show();
    }

    return 0;
    // return (GJB_PRI_PASS());
}